package com.cws.project.cws.resit.controller;


import com.cws.common.utils.StringUtils;
import com.cws.common.utils.poi.ExcelUtil;
import com.cws.framework.aspectj.lang.annotation.Log;
import com.cws.framework.aspectj.lang.enums.BusinessType;
import com.cws.framework.web.controller.BaseController;
import com.cws.framework.web.domain.AjaxResult;
import com.cws.framework.web.page.TableDataInfo;
import com.cws.project.cws.clazz.domain.ClazzUser;
import com.cws.project.cws.clazz.service.IClazzUserService;
import com.cws.project.cws.course.domain.Course;
import com.cws.project.cws.course.service.ICourseService;
import com.cws.project.cws.resit.domain.CourseResit;
import com.cws.project.cws.resit.service.ICourseResitService;
import com.cws.project.system.user.domain.User;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 补考信息Controller
 *
 * @author xyl
 * @date 2022-03-22
 */
@Controller
@RequestMapping("/cws/resit")
public class CourseResitController extends BaseController {
    private String prefix = "cws/resit";

    @Autowired
    private ICourseResitService courseResitService;
    @Autowired
    private ICourseService courseService;
    @Autowired
    private IClazzUserService clazzUserService;

    @RequiresPermissions("cws:resit:view")
    @GetMapping()
    public String resit() {
        return prefix + "/resit";
    }

    /**
     * 查询补考信息列表
     */
    @RequiresPermissions("cws:resit:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(CourseResit courseResit) {
        User sysUser = getSysUser();
        //辅导员 查看自己班的
        if(sysUser.isInstructor() || sysUser.isStudent()){
            ClazzUser clazzUser = new ClazzUser();
            clazzUser.setUserId(sysUser.getUserId());
            courseResit.setClazzIds(clazzUserService.selectClazzUserList(clazzUser).stream().map(ClazzUser::getClazzId).collect(Collectors.toList()));
        }
        //教师
        if(sysUser.isTeacher() ){
            courseResit.setTeacherId(sysUser.getUserId());
        }
        //学生
        if(sysUser.isStudent()){
            courseResit.setStudentId(sysUser.getUserId());
        }
        startPage();
        List<CourseResit> list = courseResitService.selectCourseResitList(courseResit);
        return getDataTable(list);
    }

    /**
     * 导出补考信息列表
     */
    @RequiresPermissions("cws:resit:export")
    @Log(title = "补考信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(CourseResit courseResit) {
        List<CourseResit> list = courseResitService.selectCourseResitList(courseResit);
        ExcelUtil<CourseResit> util = new ExcelUtil<>(CourseResit.class);
        return util.exportExcel(list, "resit");
    }

    /**
     * 新增补考信息
     */
    @GetMapping("/add/{courseId}")
    public String add(@PathVariable Long courseId, ModelMap mmap) {
        mmap.put("courseId",courseId);
        return prefix + "/add";
    }

    /**
     * 新增保存补考信息
     */
    @RequiresPermissions("cws:resit:add")
    @Log(title = "补考信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(CourseResit courseResit) {
        //验证是否已经结课
        Course course = courseService.selectCourseById(courseResit.getCourseId());
        if(!StringUtils.equals(course.getIsCourseEnd(),"是")){
            return error("此课程未结课，不可开启补考！！");
        }
        //验证是否已经发起补考
        CourseResit resit = courseResitService.selectCourseResitByCourseId(courseResit.getCourseId());
        if(resit != null){
            return error("此课程已经发起补考，请勿重复发起！！");
        }
        courseResit.setCreateBy(getLoginName());
        courseResit.setStudentId(getUserId());
        return toAjax(courseResitService.insertCourseResit(courseResit));
    }

    /**
     * 修改补考信息
     */
    @GetMapping("/edit/{resitId}")
    public String edit(@PathVariable("resitId") Long resitId, ModelMap mmap) {
        CourseResit courseResit = courseResitService.selectCourseResitById(resitId);
        mmap.put("courseResit", courseResit);
        return prefix + "/edit";
    }

    /**
     * 修改保存补考信息
     */
    @RequiresPermissions("cws:resit:edit")
    @Log(title = "补考信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(CourseResit courseResit) {
        return toAjax(courseResitService.updateCourseResit(courseResit));
    }

    /**
     * 删除补考信息
     */
    @RequiresPermissions("cws:resit:remove")
    @Log(title = "补考信息", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(courseResitService.deleteCourseResitByIds(ids));
    }
}
